define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'travel/order/index' + location.search,
                    add_url: 'travel/order/add',
                    edit_url: 'travel/order/edit',
                    del_url: 'travel/order/del',
                    multi_url: 'travel/order/multi',
                    import_url: 'travel/order/import',
                    table: 'travel_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"goods":__('Type goods'),"activity":__('Type activity')}, formatter: Table.api.formatter.normal},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'first_name', title: __('First_name'), operate: 'LIKE'},
                        {field: 'last_name', title: __('Last_name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'goods_type', title: __('Goods_type'), searchList: {"house":__('Goods_type house'),"car":__('Goods_type car'),"trip":__('Goods_type trip')}, formatter: Table.api.formatter.normal},
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'goods_title', title: __('Goods_title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'goods_id_home', title: __('Goods_id_home')},
                        {field: 'goods_title_home', title: __('Goods_title_home'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'goods_image', title: __('Goods_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'goods_price', title: __('Goods_price'), operate:'BETWEEN'},
                        {field: 'goods_num', title: __('Goods_num')},
                        {field: 'status', title: __('Status'), searchList: {"-2":__('Status -2'),"-1":__('Status -1'),"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'total_fee', title: __('Total_fee'), operate:'BETWEEN'},
                        {field: 'pay_fee', title: __('Pay_fee'), operate:'BETWEEN'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"wechat":__('Pay_type wechat'),"alipay":__('Pay_type alipay'),"wallet":__('Pay_type wallet'),"score":__('Pay_type score')}, formatter: Table.api.formatter.normal},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'is_finish_service', title: __('Is_finish_service'), searchList: {"0":__('is_finish_service 0'),"1":__('is_finish_service 1')}, formatter: Table.api.formatter.status},
                        {field: 'passenger_num', title: __('Passenger_num')},
                        {field: 'house_info', title: __('House_info'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'car_route', title: __('Car_route'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'luggage_num', title: __('行李数')},
                        {field: 'car_type', title: __('Car_type'), searchList: Config.carTypeList, formatter: Table.api.formatter.normal},
                        {field: 'flight_number', title: __('Flight_number')},
                        {field: 'flight_time', title: __('Flight_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'flight_number_song', title: __('Flight_number_song')},
                        {field: 'flight_time_song', title: __('Flight_time_song'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},


                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'add', //唯一标识、权限标识
                                    dropdown: '', //按钮下拉分组，默认为不分组
                                    text: function(row){
                                        return row.is_finish_service==1?'已完成服务':'设为完成服务';
                                    }, //按钮显示的文字，支持function
                                    title: '我是标题', //按钮显示的文字，支持function
                                    classname: 'btn btn-info btn-xs btn-ajax', //按钮的class，支持btn-dialog/btn-ajax/btn-addtabs
                                    url: 'travel/order/set_finish_service', //按钮的链接，支持使用{字段名}来占位替换，支持function
                                    data:{params:{type:1}},
                                    confirm: '是否确定操作？', //点击按钮后的确认框，支持function
                                    refresh: true, //操作完成后是否刷新列表
                                    disable: function(row){
                                        return row.is_finish_service==1?true:false;
                                    }, //判断按钮是否禁用，支持function
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                var that = this;
                var data = that ? $(that).data() : {};
                var ids = Table.api.selectedids(table);
                ids = ($.isArray(ids) ? ids.join(",") : ids);
                var url = data.url;
                options = {url: url, data: {ids: ids}};
                Fast.api.ajax(options, function (data, ret) {
                    table.trigger("uncheckbox");
                    var success = $(that).data("success") || $.noop;
                    if (typeof success === 'function') {
                        if (false === success.call(that, data, ret)) {
                            return false;
                        }
                    }
                    table.bootstrapTable('refresh');
                }, function (data, ret) {
                    var error = $(that).data("error") || $.noop;
                    if (typeof error === 'function') {
                        if (false === error.call(that, data, ret)) {
                            return false;
                        }
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
